(function() {
  $(function() {
    var do_update, options, run_chart;
    run_chart = echarts.init(document.getElementById('run_chart'));
    options = {
      color: ['grey', '#3399ff', '#ff3399'],
      title: {
        text: '当天交易走势',
        subtext: '按分钟统计，不包含无交易时间',
        left: 'center',
        top: 10,
        textStyle: {
          fontSize: 16,
          color: '#666'
        }
      },
      grid: {
        show: false,
        z: 0,
        left: 50,
        right: 30,
        top: 5,
        bottom: 80
      },
      tooltip: {
        formatter: function(params) {
          var time;
          if (params.data.value[0]) {
            time = params.data.value[0].split(' ');
            return time[1] + " ：" + params.data.value[1] + ' 笔';
          } else {
            return params.data.value;
          }
        },
        textStyle: {
          fontWeight: 'bold'
        }
      },
      toolbox: {
        show: true,
        right: 36,
        top: 10,
        feature: {
          saveAsImage: {}
        }
      },
      legend: {
        right: 80,
        top: 10,
        orient: 'vertical',
        data: ['全部交易', '清算交易', '失败交易']
      },
      xAxis: {
        type: 'time',
        splitLine: {
          show: false
        }
      },
      yAxis: {
        type: 'value',
        splitLine: {
          lineStyle: {
            color: '#ddd'
          }
        }
      },
      series: [
        {
          name: '全部交易',
          type: 'line',
          lineStyle: {
            normal: {
              width: 1,
              shadowColor: 'rgba(0, 0, 0, 0.3)',
              shadowBlur: 10
            }
          },
          markLine: {
            data: [
              {
                type: 'average',
                name: '均值'
              }
            ]
          }
        }, {
          name: '清算交易',
          type: 'line',
          lineStyle: {
            normal: {
              width: 1,
              shadowColor: 'rgba(0, 0, 0, 0.3)',
              shadowBlur: 10
            }
          },
          markPoint: {
            data: [
              {
                type: 'max',
                name: '峰值'
              }
            ],
            symbolSize: 40
          },
          markLine: {
            data: [
              {
                type: 'average',
                name: '均值'
              }
            ]
          }
        }, {
          name: '失败交易',
          type: 'line',
          lineStyle: {
            normal: {
              width: 2,
              shadowColor: 'rgba(0, 0, 0, 0.3)',
              shadowBlur: 10
            }
          },
          markPoint: {
            data: [
              {
                type: 'max',
                name: '峰值'
              }
            ],
            symbolSize: 40
          }
        }
      ],
      dataZoom: [
        {
          type: 'slider',
          start: 0,
          end: 100,
          backgroundColor: 'rgba(128, 128, 128, 0.3)',
          dataBackgroundColor: 'rgba(128, 128, 128, 1.0)'
        }, {
          type: 'inside',
          start: 0,
          end: 100
        }
      ]
    };
    run_chart.setOption(options);
    do_update = function() {
      run_chart.showLoading({
        text: '正在更新 ...',
        maskColor: 'rgba(0,0,0,0.0)'
      });
      return $.post('/index/rundata', {}, function(resp) {
        var zoom_start;
        run_chart.hideLoading();
        if (!resp.succ) {
          return;
        }
        zoom_start = (1 - 30 / resp.all_data.length) * 100;
        if (zoom_start < 0) {
          zoom_start = 0;
        }
        return run_chart.setOption({
          series: [
            {
              name: '全部交易',
              data: resp.all_data
            }, {
              name: '清算交易',
              data: resp.settl_data
            }, {
              name: '失败交易',
              data: resp.error_data
            }
          ],
          dataZoom: [
            {
              type: 'slider',
              start: zoom_start,
              end: 100
            }, {
              type: 'inside',
              start: zoom_start,
              end: 100
            }
          ]
        });
      });
    };
    do_update();
    setInterval(function() {
      if (Cookies.get('index.auto_refresh') !== 'off') {
        return do_update();
      }
    }, 1000 * 60);
    $('.checkbox#auto_refresh').checkbox({
      onChecked: function() {
        return Cookies.set('index.auto_refresh', 'on', {
          expires: 365,
          path: '/'
        });
      },
      onUnchecked: function() {
        return Cookies.set('index.auto_refresh', 'off', {
          expires: 365,
          path: '/'
        });
      }
    });
    if (Cookies.get('index.auto_refresh') !== 'off') {
      return $('.checkbox#auto_refresh').checkbox('set checked');
    } else {
      return $('.checkbox#auto_refresh').checkbox('set unchecked');
    }
  });

}).call(this);
